package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallWebArticleMapper;
import org.linlinjava.litemall.db.domain.LitemallWebArticle;
import org.linlinjava.litemall.db.domain.LitemallWebArticleExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallWebArticleService {
    @Resource
    private LitemallWebArticleMapper webArticleMapper;
    private LitemallWebArticle.Column[] CHANNEL = {LitemallWebArticle.Column.id, LitemallWebArticle.Column.title};


    public LitemallWebArticle findById(Integer id) {
        return webArticleMapper.selectByPrimaryKey(id);
    }

    public List<LitemallWebArticle> list(Integer id, String name,Integer cid,Integer page, Integer limit, String sort, String order) {
        List<LitemallWebArticle> articleList = selectByExampleWithBLOBs(id, name,cid, page, limit, sort, order);
        return articleList;
    }

    public List<LitemallWebArticle> querySelective(Integer id, String name, Integer page, Integer size, String sort, String order) {
        LitemallWebArticleExample example = new LitemallWebArticleExample();
        LitemallWebArticleExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andTitleLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return webArticleMapper.selectByExample(example);
    }
    public List<LitemallWebArticle> selectByExampleWithBLOBs(Integer id, String name,Integer cid, Integer page, Integer size, String sort, String order) {
        LitemallWebArticleExample example = new LitemallWebArticleExample();
        LitemallWebArticleExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(cid)) {
            criteria.andCidEqualTo(cid);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andTitleLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return webArticleMapper.selectByExampleWithBLOBs(example);
    }
    public int updateById(LitemallWebArticle article) {
        article.setUpdateTime(LocalDateTime.now());
        return webArticleMapper.updateByPrimaryKeySelective(article);
    }

    public void deleteById(Integer id) {
        webArticleMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallWebArticle article) {
        article.setAddTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        webArticleMapper.insertSelective(article);
    }

    public List<LitemallWebArticle> queryChannel() {
        LitemallWebArticleExample example = new LitemallWebArticleExample();
        example.or().andDeletedEqualTo(false);
        return webArticleMapper.selectByExampleSelective(example, CHANNEL);
    }

    public List<LitemallWebArticle> getIndexArticle() {
        LitemallWebArticleExample example = new LitemallWebArticleExample();
        LitemallWebArticleExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIsIndexShowEqualTo(true);//true ：1
        example.setOrderByClause("add_time desc");
        return webArticleMapper.selectByExampleWithBLOBs(example);
    }
}
